import { Boom } from "./Boom";

const { regClass, property } = Laya;

@regClass()
export class GameManger extends Laya.Script {
  //declare owner : Laya.Sprite3D;
  //declare owner : Laya.Sprite;

  @property(String)
  public Canvas: Laya.Node = null;
  public fruitArrLoad: Array<any> = [];
  public Y: number = 70;
  public fruit: Laya.Sprite = null;
  public isMouse: boolean = false;
  public isPlaying = true;
  public MoonTop:Laya.Node=null;
  //组件被激活后执行，此时所有节点和组件均已创建完毕，此方法只执行一次
  onAwake(): void {
    let fruitArr = [
      {
        url: "prefabs/shanzhu.lh", type: Laya.Loader.HIERARCHY
      },
      {
        url: "prefabs/pingguo.lh", type: Laya.Loader.HIERARCHY
      },
      {
        url: "prefabs/chengzi.lh", type: Laya.Loader.HIERARCHY
      },
      {
        url: "prefabs/ningmeng.lh", type: Laya.Loader.HIERARCHY
      },
      {
        url: "prefabs/mihoutao.lh", type: Laya.Loader.HIERARCHY
      },
      {
        url: "prefabs/fanqie.lh", type: Laya.Loader.HIERARCHY
      },
      {
        url: "prefabs/shuimitao.lh", type: Laya.Loader.HIERARCHY
      },
      {
        url: "prefabs/boluo.lh", type: Laya.Loader.HIERARCHY
      },
      {
        url: "prefabs/yezi.lh", type: Laya.Loader.HIERARCHY
      },
      {
        url: "prefabs/xigua.lh", type: Laya.Loader.HIERARCHY
      },
      {
        url: "prefabs/daxigua.lh", type: Laya.Loader.HIERARCHY
      }
    ];
    Laya.loader.load(fruitArr).then((res) => {
      for (const key in res) {
        if (res[key]) {
          this.fruitArrLoad[key] = res[key];
        }
      }
      this.fruit = this.createFruit(this.getRandomInt(1, 5), this.Canvas.getChildByName('Bg').width / 2, this.Y);
    });
    this.Canvas = this.owner.getChildByName('Canvas');
    Laya.stage.on(Laya.Event.MOUSE_DOWN, this, this.mouseDown);
    Laya.stage.on(Laya.Event.MOUSE_UP, this, this.mouseUp);
    Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this.mouseMove);
    this.MoonTop=this.Canvas.getChildByName('Bg').getChildByName('MoonTop');
    this.MoonTop.on(Laya.Event.TRIGGER_ENTER, this, function (other:any) {
      this.gameOver();
    });
  }
  /**
   * 
   * @param {水果的下标} fruitId
   * @param {x轴的位置} x 
   * @param {y轴的位置} y 
   */
  createFruit(fruitId: number, x: number, y: number):any{
    if (!this.isPlaying) {
      return;
    }
    let fruit: Laya.Sprite = this.fruitArrLoad[fruitId].create();
    fruit.pos(x, y);
    fruit.getComponent(Laya.RigidBody).gravityScale = 0;
    fruit.addComponent(Boom).init(fruitId, this.resetFurit);
    this.Canvas.getChildByName('Bg').addChild(fruit);
    return fruit || this.createFruit(fruitId, x, y);
  }
  /**
   * 
   * @param {生成值的最小值} min 
   * @param {生成值的最大值} max 
   * @returns 
   */
  getRandomInt(min: number, max: number) {
    return Math.floor((Math.random() * (max - min + min)) + min);
  }
  mouseDown(event: Laya.Event) {
    this.isMouse = true;
    if (this.fruit && this.isMouse) {
      this.fruit.x = event.stageX;
    }
  }
  mouseUp() {
    if (!this.isPlaying) {
      return;
    }
    this.isMouse = false;
    if (this.fruit) {
      this.fruit.getComponent(Laya.RigidBody).gravityScale = 10;
      setTimeout(() => {
        this.fruit = null;
      }, 0);
      Laya.timer.once(500, this, () => {
        this.fruit = this.createFruit(this.getRandomInt(1, 5), this.Canvas.getChildByName('Bg').width / 2, this.Y);
      });
    }
  }
  mouseMove(event: Laya.Event) {
    if (this.fruit && this.isMouse) {
      this.fruit.x = event.stageX;
    }
  }
  resetFurit(owner: Laya.Sprite, id: number) {
    let that: Laya.Sprite = this.owner.parent.parent.parent.getComponent(GameManger);
    let Bg = this.owner.parent;
    if (id == 11) {
      that.gameOver();
      return;
    }
    let fruit: Laya.Sprite = that.fruitArrLoad[id].create();
    fruit.pos(this.owner.x, this.owner.y);
    fruit.addComponent(Boom).init(id, that.resetFurit);
    Bg.addChild(fruit);
  }
  gameOver() {
    this.Canvas.getChildByName('Bg').getChildByName('Over').visible = true;
    this.isPlaying = false;
    Laya.stage.once(Laya.Event.MOUSE_DOWN, this, this.mouseDown);
    Laya.stage.once(Laya.Event.MOUSE_UP, this, this.mouseUp);
    Laya.stage.once(Laya.Event.MOUSE_MOVE, this, this.mouseMove);
  }
}